package com.music.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.music.entity.User;

public class SecurityInterceptor extends HandlerInterceptorAdapter {
	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		User user = (User) request.getSession().getAttribute("user");
		if (user == null || user.getUserRole().getId() !=2) {
			String logintUrl = request.getContextPath() + "/" + "login";
			String returnUrl = request.getServletPath();
			System.out.println("*********************************************目的地"+returnUrl);
			response.sendRedirect(logintUrl + "?returnUrl=" + returnUrl);
			return false;
		}
		return true;

	}
}
